package com.jprocms.module.cms.controller.portal.api;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageResult;
import com.jprocms.module.cms.controller.admin.link.vo.LinkListReqVO;
import com.jprocms.module.cms.controller.admin.link.vo.LinkPageReqVO;
import com.jprocms.module.cms.controller.admin.link.vo.LinkRespVO;
import com.jprocms.module.cms.convert.link.LinkConvert;
import com.jprocms.module.cms.dal.dataobject.link.LinkDO;
import com.jprocms.module.cms.service.link.LinkService;
import com.jprocms.module.cms.service.site.SiteService;
import com.jprocms.module.cms.util.CmsContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.jpro.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jpro.framework.common.pojo.CommonResult.success;
import static com.jprocms.module.cms.constant.CmsSysConstants.CMS_LIST_MAX_COUNT;
import static com.jprocms.module.cms.constant.CmsSysConstants.FRONT_API;
import static com.jprocms.module.cms.enums.ErrorCodeConstants.GUESTBOOK_NOT_EXISTS;

/**
 * 友情链接 Controller
 *
 * @author jprocms
 */
@Tag(name = "友情链接接口", description = "友情链接接口")
@RestController
@RequestMapping({FRONT_API + "/link"})
public class LinkApiController {
    private final LinkService service;
    private final SiteService siteService;

    public LinkApiController(LinkService service, SiteService siteService) {
        this.service = service;
        this.siteService = siteService;
    }

    @Operation(summary = "获取友情链接列表（ZLinkList标签）")
    @GetMapping
    public CommonResult<List<LinkRespVO>> list(LinkListReqVO reqVO) {
        if (reqVO.getSiteId() == null) {
            reqVO.setSiteId(CmsContextUtil.getCurrSiteId());
        }
        if (reqVO.getCount() > CMS_LIST_MAX_COUNT) {
            reqVO.setCount(CMS_LIST_MAX_COUNT);
        }
        final List<LinkDO> linkList = service.getLinkList(reqVO);
        final List<LinkRespVO> linkRespVOS = LinkConvert.INSTANCE.convertList(linkList);
        return success(linkRespVOS);
    }

    @Operation(summary = "获取友情链接分页（ZLinkPage标签）")
    @GetMapping("/page")
    public CommonResult<PageResult<LinkRespVO>> page(LinkPageReqVO reqVO) {
        if (reqVO.getSiteId() == null) {
            reqVO.setSiteId(CmsContextUtil.getCurrSiteId());
        }
        if (reqVO.getPageSize() > CMS_LIST_MAX_COUNT) {
            reqVO.setPageSize(CMS_LIST_MAX_COUNT);
        }
        final PageResult<LinkDO> linkPage = service.getLinkPage(reqVO);
        final PageResult<LinkRespVO> linkRespVOS = LinkConvert.INSTANCE.convertPage(linkPage);
        return success(linkRespVOS);
    }

    @Operation(summary = "获取友情链接详情")
    @ApiResponses(value = {@ApiResponse(description = "获取友情链接详情")})
    @GetMapping("/{id:[\\d]+}")
    public CommonResult<LinkRespVO> getDetail(@Parameter(description = "获取友情链接详情ID") @PathVariable Long id) {
        LinkDO bean = service.getLink(id);
        if (bean == null) {
            throw exception(GUESTBOOK_NOT_EXISTS);
        }
        return CommonResult.success(LinkConvert.INSTANCE.convert(bean));
    }

}
